﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Net;
using System.IO;
using System.ServiceModel.Web;
using System.ServiceModel;

namespace TestWebServer
{
	class Program
	{
		static HttpListener listener;

		static void Main(string[] args)
		{
			WcfServer();
			HttpServer();
		}

		static void WcfServer()
		{
			//Uri baseAddress = new Uri("http://localhost:8000/");
			//WebServiceHost svcHost = new WebServiceHost(typeof(CalcService), baseAddress);
			string address = "http://localhost:8888/DataService.svc";
			WebServiceHost svcHost = new WebServiceHost(typeof(BLServer.DataService));
			svcHost.AddServiceEndpoint(typeof(BLServer.IDataService), new BasicHttpBinding(), address);

			try
			{
				svcHost.Open();

				Console.WriteLine("Service is running");
				//Console.WriteLine("Press enter to quit...");

				//call the service
				ChannelFactory<BLServer.IDataService> scf;
				scf = new ChannelFactory<BLServer.IDataService>(new BasicHttpBinding(), address);
				BLServer.IDataService calc = scf.CreateChannel();
				string resp = calc.Login("Joe", "black");				
				(calc as ICommunicationObject).Close();
				//channel closed

				//Console.ReadLine();

				//svcHost.Close();
			}
			catch (CommunicationException cex)
			{
				Console.WriteLine("An exception occurred: {0}", cex.Message);
				svcHost.Abort();
			}
		}

		static void HttpServer()
		{
			if (!HttpListener.IsSupported)
			{
				Console.WriteLine("Windows XP SP2 or Server 2003 is required to use the HttpListener class.");
				return;
			}

			//string[] prefixes = { "http://*:8080/", "http://+:8080/", "http://+/", "http://*/" };
			string[] prefixes = { "http://localhost:8888/" };

			// URI prefixes are required,
			// for example "http://contoso.com:8080/index/".
			if (prefixes == null || prefixes.Length == 0)
				throw new ArgumentException("prefixes");

			// Create a listener.
			listener = new HttpListener();
			// Add the prefixes.
			foreach (string s in prefixes)
			{
				listener.Prefixes.Add(s);
			}
			listener.Start();
			Listen();
			listener.Stop();
			//Console.WriteLine("Recieved...");
			//Console.Read();
		}

		static void Listen()
		{
			while (true)
			{
				Console.WriteLine("Listening...");
				// Note: The GetContext method blocks while waiting for a request. 
				HttpListenerContext context = listener.GetContext();
				HttpListenerRequest request = context.Request;
				// Obtain a response object.
				HttpListenerResponse response = context.Response;

				// Construct a response.
				//string responseString = "<HTML><BODY> Hello world!</BODY></HTML>";
				string responseString = "";				
				byte[] buffer;
				if (request.RawUrl == "/")
				{
					responseString = File.ReadAllText("content/TestyTestPage.html", Encoding.Default);
					//buffer = System.Text.Encoding.UTF8.GetBytes(responseString);                    
				}
				else if (request.RawUrl == "/Silverlight.js")
				{
					responseString = File.ReadAllText("content/Silverlight.js", Encoding.Default);
				}
				else if (request.RawUrl == "/ClientBin/Testy.xap")
				{
					//response.ContentType = "application/x-silverlight-2";                    
					responseString = File.ReadAllText("content/Testy.xap", Encoding.Default);
				}

				// Get a response stream and write the response to it.
				buffer = System.Text.Encoding.Default.GetBytes(responseString);
				response.ContentLength64 = buffer.Length;
				System.IO.Stream output = response.OutputStream;
				output.Write(buffer, 0, buffer.Length);
				// You must close the output stream.
				output.Close();
			}
		}
	}
}
